package inherit;

//继承
//父类
class Animal {
    public String name;
    public void sleep() {
        System.out.println(name+"正在睡觉");
    }
}
//Dog子类
class Dog extends Animal {
    //子类独有的属性和方法
    public boolean loyal;
    public void judge() {
        System.out.println("dog " + name + " 忠诚:" + loyal);//Cat子类继承了父类Animal的属性name
    }
}
//Cat子类
class Cat extends Animal {
    //子类独有的方法
    public void catchMouse() {
        System.out.println("cat " + name + " 在抓老鼠");//Cat子类继承了父类Animal的属性name
    }
}

public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog();
        Cat cat = new Cat();
        //子类单独有的方法
        dog.judge();
        cat.catchMouse();
        //子类从父类继承的方法
        dog.sleep();
        cat.sleep();
    }
}

//super访问成员
//父类
class Animal {
    public String name;
    public void sleep() {
        System.out.println(name+"正在睡觉");
    }
}
//Dog子类
class Dog extends Animal {
    public String name="旺财";
    //子类独有的属性和方法
    public boolean loyal;

    public void func() {
        System.out.println(super.name);//super访问父类成员变量
        super.sleep();  //super访问父类成员方法
    }
}
public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.func();
    }
}

//构造方法
//父类
class Animal {
    public String name;
    public void sleep() {
        System.out.println(name+"正在睡觉");
    }

    //默认提供的父类构造方法
//    public Animal() {
//
//    }
    public Animal(String name) {
        this.name = name;
    }
}
//Dog子类
class Dog extends Animal {
    public String name;
    //子类独有的属性和方法
    public boolean loyal;

    //默认提供的子类构造方法
//    public Dog() {
//        super();
//    }
    public Dog(String name1, boolean loyal) {
        super("旺财");       //1、帮助父类进行初始化，如果父类有name和age两个值可以：super(name,age)
        this.name = name1; //2、子类自己初始化
        this.loyal = loyal;
    }

    public void func() {
        System.out.println(super.name + "是忠诚的: " + loyal);
        System.out.println(this.name + "是忠诚的: " + loyal);

    }
}
public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog("hello",true);
        dog.func();
    }
}
